package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.result.PageResult;
import com.sky.vo.DishVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
@Slf4j
@Service
public class DishServiceImpl implements com.sky.service.DishService{
    @Autowired
    private com.sky.mapper.DishMapper dishMapper;
    @Autowired
    private com.sky.mapper.DishFlavorsMapper dishFlavorsMapper;
    @Autowired
    private com.sky.mapper.SetMealMapper setMealMapper;
    @Override
    @Transactional
    public void save(DishDTO dishDTO) {
        //关联的口味也要保存
        //菜品表插入数据
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        log.info("dish:{}",dish);
        dishMapper.insert(dish);
        Long dishId = dish.getId();
        //口味表插入数据
        List<DishFlavor> dishFlavors = dishDTO.getFlavors();
        if (dishFlavors == null){
            return ;
        }
        for (DishFlavor dishFlavor : dishFlavors) {
            dishFlavor.setDishId(dishId);
        }
        //批量插入
        dishFlavorsMapper.insertList(dishFlavors);
    }

    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> dishPage =dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(dishPage.getTotal(),dishPage.getResult());
    }



    @Transactional
    @ApiOperation("value = \"根据id批量删除菜品信息\"")
    public boolean delete(List<Long> ids) {
        //起售中或套餐中
        for(Long id:ids){
            log.info("状态查询");
            if(dishMapper.queryStatus(id)==1){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        log.info("套餐查询");
        if(setMealMapper.queryMeal(ids)!=0){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        //批量删除
        log.info("删除菜品");
        dishMapper.deleteList(ids);
        //删除口味
        log.info("删除口味");
        System.out.println("Passed dishIds: " + ids);
        int count = setMealMapper.queryMeal(ids);
        System.out.println("Query result: " + count);

        return true;
    }

    @Override
    public DishVO getById(Long id) {
        Dish dish = dishMapper.selectById(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavorsMapper.queryById(id));
        return dishVO;
    }

    @Transactional
    @Override

    public void update(DishDTO dishDTO) {
        //插入菜品
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);
        //插入口味,先删除，再插入
        log.info("删除口味");
        dishFlavorsMapper.deleteById(dish.getId());
        List<DishFlavor> dishFlavors = dishDTO.getFlavors();
        if (dishFlavors != null && dishFlavors.size()>0) {
            for (DishFlavor dishFlavor : dishFlavors) {
                dishFlavor.setDishId(dish.getId());
            }
            log.info("插入口味");
            //批量插入
            dishFlavorsMapper.insertList(dishFlavors);
        }

    }

    @Override
    public void updateStatus(Integer status, Long id) {
        dishMapper.updateStatus(status,id);
    }

    @Override
    public List<DishVO> getBycategoryId(Long categoryId) {
        return dishMapper.getBycategoryId(categoryId);
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorsMapper.queryById(d.getId());
            System.out.println("\ndishVOList:"+dishVOList+"\n");
            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

}
